package com.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dao.SysUserMapper;
import com.entity.SysUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.SysUserService;
import com.utils.BaseService;
import com.utils.DataTables;
import com.utils.RequestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author lanouhn
 */
@Service
public class SysUserServiceImpl extends BaseService implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public long getSysUserRowCount() {
        return sysUserMapper.getSysUserRowCount();
    }

    @Override
    public List<SysUser> selectSysUser() {
        return sysUserMapper.selectSysUser();
    }

    @Override
    public SysUser selectSysUserByObj(SysUser obj) {
        return sysUserMapper.selectSysUserByObj(obj);
    }

    @Override
    public SysUser selectSysUserById(Integer id) {
        return sysUserMapper.selectSysUserById(id);
    }

    @Override
    public int insertSysUser(SysUser value) {
        return sysUserMapper.insertSysUser(value);
    }

    @Override
    public int insertNonEmptySysUser(SysUser value) {
        return sysUserMapper.insertNonEmptySysUser(value);
    }

    @Override
    public int insertSysUserByBatch(List<SysUser> value) {
        return sysUserMapper.insertSysUserByBatch(value);
    }

    @Override
    public int deleteSysUserById(Integer id) {
        return sysUserMapper.deleteSysUserById(id);
    }

    @Override
    public int updateSysUserById(SysUser enti) {
        return sysUserMapper.updateSysUserById(enti);
    }

    @Override
    public int updateNonEmptySysUserById(SysUser enti) {
        return sysUserMapper.updateNonEmptySysUserById(enti);
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("sort asc");

        //判断需要排序的类是否为空
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(SysUserMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(sysUserMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL(),dataTables.getTimeMin(),dataTables.getTimeMax()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<SysUser>());

        return dataTables;
    }

    @Override
    public Map<String, Object> addSysUser(SysUser sysUser) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysUserMapper.insertSysUser(sysUser);
//        System.out.println(id);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        } else {
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> updateSysUser(SysUser sysUser) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysUserMapper.updateNonEmptySysUserById(sysUser);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        } else {
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> deleteByPrimaryKeys(List<Integer> idList) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysUserMapper.deleteByPrimaryKeys(idList);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        } else {
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    public SysUserMapper getSysUserMapper() {
        return this.sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

}